package com.itheima.web.controller.system;

import com.github.pagehelper.PageInfo;
import com.itheima.common.utils.UtilFuns;
import com.itheima.domain.system.Dept;
import com.itheima.domain.system.Role;
import com.itheima.domain.system.User;
import com.itheima.service.system.IDeptService;
import com.itheima.service.system.IRoleService;
import com.itheima.service.system.IUserService;
import com.itheima.web.controller.BaseController;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/system/user")
public class UserController extends BaseController {

    @Autowired
    private IUserService userService;

    @Autowired
    private IDeptService deptService;

    @Autowired
    private IRoleService roleService;

    @RequestMapping(value = "/list",name = "分页查询用户数据")
    public String findAll(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "5") int size) {

        PageInfo pageInfo = userService.findAll(super.getCurrentUserCompanyId(), page, size);

        request.setAttribute("page", pageInfo);

        return "system/user/user-list";
    }

    @RequestMapping(value = "/edit",name = "保存或者更新用户")
    public String edit(User user) {
        //1.设置所属企业信息
        user.setCompanyId(super.getCurrentUserCompanyId());
        user.setCompanyName(super.getCurrenUserCompanyName());
        //判断保存或更新
        if (UtilFuns.isEmpty(user.getId())) {
            //保存
            userService.save(user);
        } else {
            //更新
            //从数据库中获取数据
            User byId = userService.findById(user.getId());

            //如果有数据就替换数据库数据，如果没有就使用从数据库获取的数据
            //替换掉不想替换的数据，companyId，companyName
            BeanUtils.copyProperties(user, byId, new String[]{"companyId", "companyName"});

            //更新数据
            userService.update(user);
        }
        //重定向跳转
        return "redirect:/system/user/list.do";
    }

    @RequestMapping(value = "/toAdd",name = "去到添加用户页面")
    public String toAdd() {
        //查询改企业下的所有部门
        List<Dept> list = deptService.findAll(super.getCurrentUserCompanyId());
        request.setAttribute("deptList", list);
        //转发到页面
        return "system/user/user-add";
    }

    @RequestMapping(value = "/toUpdate",name = "去到更新用户页面")
    public String toUpdate(String id) {
        //数据回显
        User user = userService.findById(id);
        request.setAttribute("user", user);

        //查询该企业下的所有部门
        List<Dept> list = deptService.findAll(super.getCurrentUserCompanyId());
        request.setAttribute("deptList", list);

        return "system/user/user-update";
    }

    @RequestMapping(value = "/delete",name = "删除用户")
    public String delete(String id) {
        userService.delete(id);
        return "redirect:/system/user/list.do";
    }

    @RequestMapping(value = "/roleList",name = "去用户的角色分配列表")
    public String roleList(String id) {
        //数据回显
        User user = userService.findById(id);
        request.setAttribute("user", user);

        //查询所有角色
        List<Role> roleList = roleService.findAll(super.getCurrentUserCompanyId());
        request.setAttribute("roleList", roleList);

        //根据用户id查询所有具备的角色
        //只需要查询角色用户中间表即可
        List<String> userRoleList = userService.findUserRole(id);
        request.setAttribute("userRoleStr", userRoleList.toString());

        return "system/user/user-role";
    }

    @RequestMapping(value = "/changeRole",name = "给用户分配角色")
    public String changeRole(@RequestParam("userid") String userId,String[] roleIds){
        //调用业务层执行用户的角色分配
        userService.saveUserRole(userId,roleIds);
        //重定向list。do请求
        return "redirect:/system/user/list.do";
    }
}
